package com.sprouting.personal.service;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sprouting.personal.dao.PsNotepadDao;
import com.sprouting.personal.dao.UploadFileDao;
import com.sprouting.personal.interfaces.common.Constant;
import com.sprouting.personal.model.entity.PsNotepad;
import com.sprouting.personal.model.entity.UploadFile;
import com.sprouting.personal.model.vo.PsNotepadVo;
import com.sprouting.personal.utils.EntityTool;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (PsNotepad)表服务实现类
 *
 * @author longx
 * @since 2022-12-18 23:09:17
 */
@Service
@Slf4j
public class PsNotepadService extends ServiceImpl<PsNotepadDao, PsNotepad> {

    @Resource
    private PsNotepadDao psNotepadDao;
    @Resource
    private UploadFileDao uploadFileDao;

    /**
     * 通过ID查询单条数据
     * @param id 主键
     * @return 实例对象
     */
    public PsNotepad getPsNotepadById(String id) {
        return psNotepadDao.getPsNotepadById(id);
    }

    /**
     * 分页查询记事本
     * @param page 分页参数
     * @param psNotepad 查询参数
     * @return
     */
    public Page<PsNotepadVo> getNotepadListPage(Page page, PsNotepad psNotepad) {
        return psNotepadDao.getNotepadListPage(page, psNotepad);
    }

    /**
     * 保存记事本同时将文件转为正式数据
     * @param psNotepad 记事本数据
     * @param uploadFileList 图片数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveNotepad(PsNotepad psNotepad, List<UploadFile> uploadFileList) {
        psNotepadDao.insert(psNotepad);

        // todo 优化点
        if (CollUtil.isNotEmpty(uploadFileList)){
            for (UploadFile uploadFile : uploadFileList){
                uploadFile.setTempFile(Constant.TempFile.PRODUCTION);
                EntityTool.buildUpdBase(uploadFile, StpUtil.getLoginIdAsString());

                uploadFileDao.updateById(uploadFile);
            }
        }
    }

    /**
     * 更新记事本同时转换文件
     * @param psNotepad 记事本
     * @param uploadFileList 文件
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateNotepad(PsNotepad psNotepad, List<UploadFile> uploadFileList) {
        psNotepadDao.updateById(psNotepad);

        // todo 优化点
        if (CollUtil.isNotEmpty(uploadFileList)){
            for (UploadFile uploadFile : uploadFileList){
                uploadFile.setTempFile(Constant.TempFile.PRODUCTION);
                EntityTool.buildUpdBase(uploadFile, StpUtil.getLoginIdAsString());

                uploadFileDao.updateById(uploadFile);
            }
        }
    }

    /**
     * 删除记事本
     * @param id 记事本主键
     * @param uploadFileList 图片文件
     * @param basePath 基础图片保存路径
     */
    @Transactional(rollbackFor = Exception.class)
    public void removeNotepad(String id, List<UploadFile> uploadFileList, String basePath) {
        psNotepadDao.deleteById(id);

        if (CollUtil.isNotEmpty(uploadFileList)){
            for (UploadFile uploadFile : uploadFileList){
                String savePath = uploadFile.getSavePath();

                if (FileUtil.isFile(basePath + savePath)){
                    // 删图片删数据库
                    FileUtil.del(basePath + savePath);
                    uploadFileDao.deleteById(uploadFile.getId());

                } else {
                    // 不对劲则变临时文件，后续处理
                    uploadFile.setTempFile(Constant.TempFile.TEMP);
                    uploadFile.setFileInfo("路径异常，未删除超过");
                    uploadFileDao.updateById(uploadFile);
                    log.error("要删除的是数据是目录，跳过该删除操作：{}", basePath + savePath);
                }
            }
        }
    }
}

